package com.ljdy.product.utils;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class CheckFileType {

	/**
	 * 校验图片类型
	 * @param file
	 * @return
	 */
	private static String[] pictureTypes = {"bmp","jpg","png","tiff","gif","pcx","tga","exif","fpx","svg","psd","cdr","pcd","dxf","ufo","eps","ai","raw","wmf","webp"};
	
	/**
	 * 根据文件判断是否是图片
	 * @param file
	 * @return
	 */
	public static boolean checkPicture(File file) {
		
		String suffix = getFileSuffix(file);
		return checkPicture(suffix);
	}
	
	/**
	 * 根据文件名判断是否是图片
	 * @param fileName
	 * @return
	 */
	public static boolean checkPicture(String fileName) {
		String suffix = "";
		if (fileName.lastIndexOf(".")>0) {
			//是文件名
			suffix = getFileSuffix(fileName);
		}else {
			suffix = fileName;
		}
		List<String> types = Arrays.asList(pictureTypes);
		return types.contains(suffix);
	}
	
	private static String getFileSuffix(File file) {
		return getFileSuffix(file.getName());
	}
	
	private static String getFileSuffix(String fileName) {
		return fileName.substring(fileName.lastIndexOf(".")+1);
	}
}
